package com.xiaozhen.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.common.core.domain.device.Device;
import com.xiaozhen.common.core.domain.device.queryParams.DeviceQueryParam;
import com.xiaozhen.common.core.domain.driver.ParseModbusAttribute;
import com.xiaozhen.common.core.domain.driver.ParseModbusDriver;
import com.xiaozhen.common.core.domain.driver.param.ModbusParam;
import com.xiaozhen.common.core.domain.driver.vo.ModbusDriverVo;
import com.xiaozhen.device.dao.DeviceDao;
import com.xiaozhen.device.service.DeviceService;
import com.xiaozhen.driver.dao.ParseModbusAttributeDao;
import com.xiaozhen.driver.dao.ParseModbusDriverDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/11/1 11:32
 * @description:
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public Page<Device> selectDeviceList(Page<Device> page, DeviceQueryParam queryParam) {
        return deviceDao.selectDeviceListByParam(page, queryParam);
    }

    @Override
    public Boolean checkDeviceNameUnique(Device device) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("device_id");
        queryWrapper.eq("device_name", device.getDeviceName());
        List<Device> devices = deviceDao.selectList(queryWrapper);
        if (devices.size() == 0) {
            return true;
        } else if (device.getDeviceId() != null && device.getDeviceId().equals(devices.get(0).getDeviceId())) {
            return true;
        }
        return false;
    }

    @Override
    public Integer insertDevice(Device device) {
        return deviceDao.insert(device);
    }

    @Override
    public Integer updateDevice(Device device) {
        return deviceDao.updateById(device);
    }

    @Override
    public Integer deleteDeviceById(Long deviceId) {
        return deviceDao.deleteById(deviceId);
    }

    @Autowired
    private ParseModbusDriverDao parseModbusDriverDao;
    @Autowired
    private ParseModbusAttributeDao parseModbusAttributeDao;

    @Override
    public ModbusDriverVo getModbusDeviceInfo(Long deviceId) {
        Device device = deviceDao.selectById(deviceId);
        ParseModbusDriver driver = parseModbusDriverDao.selectById(device.getDriverId());
        QueryWrapper<ParseModbusAttribute> attributeQueryWrapper = new QueryWrapper<>();
        attributeQueryWrapper.eq("driver_id", device.getDriverId());
        List<ParseModbusAttribute> attributeList = parseModbusAttributeDao.selectList(attributeQueryWrapper);
        ModbusDriverVo modbusDriverVo = new ModbusDriverVo();
        modbusDriverVo.setDevice(device);
        modbusDriverVo.setDriver(driver);
        modbusDriverVo.setAttributeList(attributeList);
        return modbusDriverVo;
    }
}
